package com.liu.netty.common;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
 * 处理报文的工具
 */
@Slf4j
public class MessageUtil {
    // 示例报文
    // ###<TaskID=00001;TaskFlag=1;TaskContent{ObjectAddress=1234;CommandStyle=2;DataID=2;DataContent<RunCycle=60;TemperatureLimit=80;SoakingTime=5>}>###

    //年月日时间格式，自带后面的时分秒
    private final String YMD_FORMAT = "yyyy-MM-dd HH:mm:ss";
    //年月日时间格式，自带后面的时分秒
    private final String YMD = "yyyy-MM-dd";

    private final String FAIL = "fail";

    private final String HEARTBEAT = "heartbeat";

    private final String SUCCESS = "success";

    private final String BEAN_NOT_FOUND = "bean not found";

    public  String  handMsg(String msg){
    if(StringUtils.isEmpty(msg)){
       return  null;
    }
   log.info("接收到的报文为：{}",msg);
   if(msg.startsWith("###<")&& msg.endsWith(">###")){

   }else {
       return null;
   }

        String TaskID = "";
        String TaskFlag = "";
        String CommandStyle = "";
        String DataID = "";
        String ObjectAddress = "";
//     抽取 获得taskid 和taskFlag
        getTaskIdAndTaskFlag(TaskID,TaskFlag,msg);
//     抽取 参数
//
//
        String dataContent= getCommandStyleAndObjectAddressAndDataID(CommandStyle,DataID,ObjectAddress,msg);
         log.info(dataContent);
        String responseFlag = "";

        if(StringUtils.isEmpty(CommandStyle)|| StringUtils.isEmpty(DataID)){
            responseFlag=FAIL;
        }else {
            handleBw(CommandStyle, DataID, dataContent, TaskID, ObjectAddress);
        }



        return  null;
    }

    private void handleBw(String commandStyle, String dataID, String dataContent, String taskID, String objectAddress) {

    }


    private  String getCommandStyleAndObjectAddressAndDataID(String CommandStyle,String DataID,String ObjectAddress,  String msg) {
        int contentStart=msg.indexOf("TaskContent{");
        int contentEnd=msg.lastIndexOf("}");
        String content =msg.substring(contentStart,contentEnd);
        log.info("获取正文：{}",content);
        String[] ss =content.split(";");
         for(String ts :ss){
            if(ts.contains("ObjectAddress=")){
                ObjectAddress = ts.substring(ts.indexOf("=")+1);
            }
            if(ts.contains("DataID")){
                DataID=ts.substring(ts.indexOf("=")+1);
            }
            if(ts.contains("CommandStyle")){
                CommandStyle=ts.substring(ts.indexOf("=")+1);
            }
        }
        int dataContentBegin = msg.indexOf("DataContent<");
        // 获取数据内容的最后index
        int dataContentEnd = msg.lastIndexOf(">}");
        String dataContent = msg.substring(dataContentBegin+12, dataContentEnd);
        return dataContent;
    }


    private  void getTaskIdAndTaskFlag(String TaskID,String TaskFlag ,String msg) {

        String[] headMsg = msg.split(";");
        for (String ss:headMsg ) {
            log.info("分割后的字符串：{}",ss);
            if(ss.contains("TaskID=")){
                String task_id=ss.substring(ss.indexOf("=")+1);
                 if(task_id.trim().length()<=7){
                     TaskID=task_id;
                 }
            }else if (ss.contains("TaskFlag=")){
                TaskFlag=ss.substring(ss.indexOf("=")+1);
            }

        }
    }


    public static void main(String[] args) {
        String msg = "###<TaskID=00001;TaskFlag=1;TaskContent{ObjectAddress=1234;CommandStyle=2;DataID=2;DataContent<RunCycle=60;TemperatureLimit=80;SoakingTime=5>}>###";
        MessageUtil util = new MessageUtil();
        util.handMsg(msg);
    }

}
